package
{
import flash.display.Graphics;
public class DeleteTool extends MoveTool
{
	
	public function DeleteTool()
	{
		super();
		keyCode=68;
	}
	
	override public function keyUp():void{
		trace("finish");
		finish();
	}
	
	override public function up():void{
		
	}
	
	override public function move():void{
	
	}
	
	override public function down():void{
		var p:Object=canvas.getClosePoint();
		var l:Array=canvas.getCloseLine();
		
		trace("delete tool down");
		if(p.isControl){
			trace("is control.");
			//delete control point
			var path:Path=canvas.getPathWithControlId(p.id);
			path.control=-1;
			//delete the point
			canvas.removePoint(p);
			
		}else if(!p.isCorner && !p.isControl && !p.isColor && !p.isHole){
			trace("!corner !control !color !hole");
			//delete point
			//remove and join path
			
			var paths:Array=canvas.getPaths(p);
			
			if(paths.length==2){
				//delete point with 2 paths
				//join them 2
				var path1:Path=paths[0];
				var path2:Path=paths[1];
				
				if(path1.from==p.id){
					if(path2.from==p.id){
						path1.from=path2.to;
					}else{
						path1.from=path2.from;
					}
				}else{
					if(path2.from==p.id){
						path1.to=path2.to;
					}else{
						path1.to=path2.from;
					}
				}
				
				//remove path2
				canvas.removePath(path2);
				//remove center point
				canvas.removePoint(p);
				
				//check is it have same path and clear it
				canvas.clearSamePath();
			}else if(paths.length==1){
				trace("paths length==1");
				//delete point with 1 path
				canvas.removePath(paths[0]);
				canvas.removePoint(p);
			}else if(paths.length==0){
				canvas.removePoint(p);
			}else{
				trace("path length can not delete:"+paths.length);
			}
		}else if(p.isColor){
			trace("is color");
			//delete color point
			canvas.removeColorPoint(p);
		}else if(p.isHole){
			trace("delete tool: is hole");
			canvas.removeHolePoint(p);
		}else{
			trace("not all.");
		}
		
	}
	override public function drawPoints(points:Collection,point_pen:Graphics):void{
		//draw points 
		var itr:Iterator=new Iterator(points.toArray());
		var point:Object;
		point_pen.clear();
		point_pen.lineStyle(1,0x000000,1);
		while(itr.hasNext()){
			point=itr.next();
			point_pen.drawRect(point.x-3,point.y-3,6,6);
		}
	}
	
}

}

